<?php

/**
 * 调用数据库的类
 * Class db
 */
class db{

    private $host = '101.34.87.12';
    private $user = 'admin';
    private $password = '123456';
    private $dbname = 'top237';
    function con(){

        $con = new mysqli($this->host,$this->user,$this->password,$this->dbname);
        $con->query("set names utf8");
        if($con->connect_error){
            $res = Array('code'=>0, 'msg'=>'数据库连接失败');
            die(json_encode($res));
        };
        return $con;
    }
    //获取诗歌本中所有的条目
    function getZmList(){

        $con = $this-> con();

        $sql = "SELECT _id,isad,isopern,mulu,shijiIndex,xuhao FROM zanmeidb ORDER BY shijiIndex ASC,xuhao ASC;";

        echo $this->getDb($sql);

    }
    //根据ID获取诗歌本中的条目详情
    function getZmItem($id){
        $sql = "select * from zanmeidb where _id = '{$id}';";
        echo $this->getDb($sql);
    }

    //获取话语中的条目
    function getHyList($type){
        $con = $this->con();
        $sql = "select _id,biaoTi,sort,type from hydb where type=\"{$type}\" order by sort desc;";
        echo $this->getDb($sql);
    }

    //根据id话语中的详情
    function getHyitem($id){
        $con = $this->con();
        $sql = "select * from hydb where _id=\"{$id}\";";
        echo $this->getDb($sql);
    }

    //使用select查询时通用方法
    function getDb($qure){ //42

        $link=$this->con();
        $result=mysqli_query($link, $qure);
        $arr=mysqli_fetch_all($result, 1);
        $json = json_encode(array(
            "code"=>1,
            "msg"=>"数据获取成功",
            "tatol"=>count($arr),
            "data"=>$arr),
            JSON_UNESCAPED_UNICODE);

        mysqli_close($link);

        return $json;

    }

}






